import os
import os.path
import user

import GlobalBSA
import BSAPalette
import BSAFont
import BSALightMap
import BSACFA
import BSADFA
import BSAVOC
import BSACIF
import Utils

class DataExtractionMain:
    PALETTE_NAMES = ['PAL.COL', 'CHARSHT.COL', 'DAYTIME.COL', 'DREARY.COL']
    PALETTE_DEFAULT = PALETTE_NAMES[0]
    PALETTE_CHARSHEET = PALETTE_NAMES[1]
    PALETTE_DAYTIME = PALETTE_NAMES[2]
    PALETTE_DREARY = PALETTE_NAMES[3]
    
    FONTS = [('FONT_A.DAT', 11), ('FONT_B.DAT', 6), ('FONT_C.DAT',14), ('FONT_D.DAT', 7),
                  ('FONT_S.DAT', 5), ('ARENAFNT.DAT', 9), ('CHARFNT.DAT', 8), ('TEENYFNT.DAT', 8)]
    LIGHTMAPS = ['FOG.LGT', 'NORMAL.LGT']
    
    CIFS_TO_NOT_LOAD = ['BRASS.CIF', 'MARBLE.CIF', 'MARBLE2.CIF']
    
    def __init__(self, base_dir = ''):
        self._base_dir = base_dir
        self._gbsa = GlobalBSA.GlobalBSA(base_dir)
        self._palettes = {}
        self._fonts = {}
        self._lightmaps = {}
        self._vocs = {}
        self._cfas = {}
        self._dfas = {}
        self._cifs = {}

    def __repr__(self):
        return "DataExtractionMain: [global_bsa=" + str(self._gbsa) + "|palettes=" +\
            str(self._palettes) + "|fonts=" + str(self._fonts) + "|lightmaps=" +\
            str(self._lightmaps) + "]"

    # gettters
    def get_palettes(self):
        return self._palettes
    
    def get_fonts(self):
        return self._fonts
    
    def get_lightmaps(self):
        return self._lightmaps
    
    def get_cfas(self):
        return self._cfas
    
    def get_dfas(self):
        return self._dfas
    
    def dump_lightmaps(self):
        base_path = os.path.join(user.home, '.cache', 'pyarenalinux', 'images')

        if not os.path.exists(base_path):
           # make the dir
           os.makedirs(base_path, 0700)
            
        for lmap_fname in self._lightmaps.iterkeys():
            lmap = self._lightmaps[lmap_fname]
            fname = os.path.join(base_path, lmap.get_name() + '.gif')
            lmap.dump(fname)
    
    def dump_vocs(self):
        base_path = os.path.join(user.home, '.cache', 'pyarenalinux', 'sounds')
        
        if not os.path.exists(base_path):
            # make the dir
            os.makedirs(base_path, 0700)
            
        for voc_filename in self.VOCS:
            voc = self._vocs[voc_filename]
            fname = os.path.join(base_path, voc_filename)

            voc.dump(fname)

    
    def dump_cfas(self):
        base_path = os.path.join(user.home, '.cache', 'pyarenalinux', 'creatures')
        
        if not os.path.exists(base_path):
            os.makedirs(base_path, 0700)

        for cfa_filename in self.CFAS:
            cfa = self._cfas[cfa_filename]
            
            (root, ext) = os.path.splitext(cfa_filename)
            cfa_filename2 = root + '.gif'
            fname = os.path.join(base_path, cfa_filename2)
            
            cfa.dump(fname)

    def dump_dfas(self):
        base_path = os.path.join(user.home, '.cache', 'pyarenalinux', 'anims')
        
        if not os.path.exists(base_path):
            os.makedirs(base_path, 0700)

        for dfa_filename in self.DFAS:
            dfa = self._dfas[dfa_filename]

            (root, ext) = os.path.splitext(dfa_filename)
            dfa_filename2 = root + '.gif'
            fname = os.path.join(base_path, dfa_filename2)
            
            dfa.dump(fname)
            
    def dump_cifs(self):
        base_path = os.path.join(user.home, '.cache', 'pyarenalinux', 'Cifs')
        
        if not os.path.exists(base_path):
            os.makedirs(base_path, 0700)

        for cif_filename in self.CIFS:
            print 'dumping: ', cif_filename
            cif = self._cifs[cif_filename]
            
            (root, ext) = os.path.splitext(cif_filename)
            cif_filename2 = root + '.gif'
            fname = os.path.join(base_path, cif_filename2)
            
            cif.dump(fname)
            
    def dump_index_resources(self):
        '''Dumps all resources to a preconfigured directory, or ~/.cache/pyarenalinux/'''
        #self.dump_lightmaps()
        #self.dump_vocs()
        #self.dump_cfas()
        #self.dump_dfas()
        #self.dump_cifs()
        pass
        
    def load_palettes(self):
        for pal_filename in self.PALETTE_NAMES:
            full_filename = os.path.join(self._base_dir, pal_filename)

            pal = BSAPalette.BSAPalette(full_filename)
            pal.load()
            self._palettes[pal_filename] = pal
    
    def load_fonts(self):
        for (font_filename, font_size) in self.FONTS:
            full_filename = os.path.join(self._base_dir, font_filename)

            font = BSAFont.BSAFont(full_filename, font_size)
            font.load()
            self._fonts[font_filename] = font

    def load_lightmaps(self):
        default_palette = self._palettes[self.PALETTE_DEFAULT]
        pal_buffer = default_palette.get_palette()
        default_palette_red_first = BSAPalette.make_palette_red_first(pal_buffer)
        
        for lightmap_filename in self.LIGHTMAPS:
            full_filename = os.path.join(self._base_dir, lightmap_filename)
            
            lightmap = BSALightMap.BSALightMap(full_filename, default_palette_red_first)
            lightmap.load()
            self._lightmaps[lightmap_filename] = lightmap
    
    def load_vocs(self):
        voc_entries = self._gbsa.get_voc_entries()
        
        self.VOCS = []
        
        for voc_entry in voc_entries:
            fname = voc_entry.get_name()
            fname = fname.upper()
            self.VOCS.append(fname)

            gbsa_data = self._gbsa.get_global_bsa_data()
            voc = BSAVOC.BSAVOC(voc_entry, gbsa_data)
            voc.load()
            self._vocs[fname] = voc
            
        self.VOCS.sort()

    def load_cfas(self):
        default_palette = self._palettes[self.PALETTE_DEFAULT]
        pal_buffer = default_palette.get_palette()
        default_palette_red_first = BSAPalette.make_palette_red_first(pal_buffer)
        
        cfa_entries = sorted(self._gbsa.get_cfa_entries())
        
        self.CFAS = []
        
        for cfa_entry in cfa_entries:
            cfa_filename = cfa_entry.get_name()
            cfa_filename = cfa_filename.upper()
            self.CFAS.append(cfa_filename)
            
            gbsa_data = self._gbsa.get_global_bsa_data()
            cfa = BSACFA.BSACFA(cfa_entry, gbsa_data, default_palette_red_first.flatten())
            cfa.load()
            self._cfas[cfa_filename] = cfa
        
        self.CFAS.sort()
        
    def load_dfas(self):
        default_palette = self._palettes[self.PALETTE_DEFAULT]
        pal_buffer = default_palette.get_palette()
        default_palette_red_first = BSAPalette.make_palette_red_first(pal_buffer)
        
        dfa_entries = sorted(self._gbsa.get_dfa_entries())
        
        self.DFAS = []
        
        for dfa_entry in dfa_entries:
            dfa_filename = dfa_entry.get_name()
            dfa_filename = dfa_filename.upper()

            self.DFAS.append(dfa_filename)
            
            gbsa_data = self._gbsa.get_global_bsa_data()
            dfa = BSADFA.BSADFA(dfa_entry, gbsa_data, default_palette_red_first.flatten())
            dfa.load()
            self._dfas[dfa_filename] = dfa
        
        self.DFAS.sort()

    def load_cifs(self):
        default_palette = self._palettes[self.PALETTE_DEFAULT]
        pal_buffer = default_palette.get_palette()
        default_palette_red_first = BSAPalette.make_palette_red_first(pal_buffer)
        
        cif_entries = sorted(self._gbsa.get_cif_entries())
        
        self.CIFS = []
        
        for cif_entry in cif_entries:
            cif_filename = cif_entry.get_name()
            cif_filename = cif_filename.upper()
            if cif_filename in self.CIFS_TO_NOT_LOAD:
                continue
            self.CIFS.append(cif_filename)
            
            gbsa_data = self._gbsa.get_global_bsa_data()
            cif = BSACIF.BSACIF(cif_entry, gbsa_data, default_palette_red_first.flatten())
            cif.load()
            self._cifs[cif_filename] = cif
        
        self.CIFS.sort()

    def load(self):
        self._gbsa.load()
        #print self._gbsa.pretty_print()
        
        self.load_palettes()
        #self.load_fonts()
        #self.load_lightmaps()
        #self.load_vocs()
        #self.load_cfas()
        #self.load_dfas()
        self.load_cifs()

        self.dump_index_resources()

